package com.puboot.module.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.puboot.common.util.CoreConst;
import com.puboot.module.admin.mapper.BizCategoryMapper;
import com.puboot.module.admin.mapper.BizTagsMapper;
import com.puboot.module.admin.model.BizCategory;
import com.puboot.module.admin.model.BizTags;
import com.puboot.module.admin.service.BizCategoryService;
import lombok.AllArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Linzhaoguan
 * @version V1.0
 * @date 2019年9月11日
 */
@Service
@AllArgsConstructor
public class BizCategoryServiceImpl extends ServiceImpl<BizCategoryMapper, BizCategory> implements BizCategoryService {

    private final BizCategoryMapper bizCategoryMapper;

    private final BizTagsMapper bizTagsMapper;

    @Override
    @Cacheable(value = "category", key = "'tree'")
    public List<BizCategory> selectCategories(BizCategory bizCategory) {
        return bizCategoryMapper.selectCategories(bizCategory);
    }

    @Override
    @CacheEvict(value = "category", allEntries = true)
    public int deleteBatch(Integer[] ids) {
        return bizCategoryMapper.deleteBatch(ids);
    }

    @Override
    public BizCategory selectById(Integer id) {
        return bizCategoryMapper.getById(id);
    }

    @Override
    public List<BizCategory> selectByPid(Integer pid) {
        return bizCategoryMapper.selectList(Wrappers.<BizCategory>lambdaQuery().eq(BizCategory::getPid, pid));
    }

    @Override
    public List<BizCategory> selectCategoriesAndTags() {

        QueryWrapper<BizCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", CoreConst.STATUS_VALID);
        queryWrapper.orderByAsc("sort");

        List<BizCategory> bizCategories = baseMapper.selectList(queryWrapper);
        for (BizCategory bizCategory : bizCategories) {
            List<BizTags> bizTags = bizTagsMapper.findByCategoryId(bizCategory.getId());
            bizCategory.setTags(bizTags);
        }
        return bizCategories;
    }
}
